package com.s63.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.s63.entity.User;
import com.s63.mapper.UserMapper;
import com.s63.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-10-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Cacheable(cacheNames = "demo", key = "'userServiceImpl.page.' + #page.current ")
    @Override
    public <E extends IPage<User>> E page(E page, Wrapper<User> queryWrapper) {
        return super.page(page, queryWrapper);
    }

//    @Cacheable(cacheNames = "demo", key = "#root.targetClass.name + '.' + #root.methodName + '.' + #id")
    @Override
    public User getById(Serializable id) {
        System.out.println("getById:" + id + "被调用了==============》");
        User user = super.getById(id);
        stringRedisTemplate.opsForZSet().incrementScore("s63-demo-user", user.getAccount(), 1);
        return user;
    }

    @Override
    public Set<?> getScores() {
        return stringRedisTemplate.opsForZSet().reverseRangeWithScores("s63-demo-user", 0, 4);
    }

    @Override
    @CacheEvict(cacheNames = "demo", allEntries = true)
//    @CachePut(cacheNames = "demo", key = "#root.targetClass.name + '.getById.' + #user.id")
    public User update(User user) {
        super.updateById(user);
        return user;
    }

    @Override
    @CacheEvict(cacheNames = "demo", allEntries = true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "demo", allEntries = true)
    @Override
    public boolean save(User entity) {
        return super.save(entity);
    }


}
